package org.dromara.agriculture.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.agriculture.domain.bo.AgricultureBaseinfoBo;
import org.dromara.agriculture.domain.vo.AgricultureBaseinfoVo;
import org.dromara.agriculture.domain.AgricultureBaseinfo;
import org.dromara.agriculture.mapper.AgricultureBaseinfoMapper;
import org.dromara.agriculture.service.IAgricultureBaseinfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 基地信息Service业务层处理
 *
 * @author Robin
 * @date 2024-12-05
 */
@RequiredArgsConstructor
@Service
public class AgricultureBaseinfoServiceImpl implements IAgricultureBaseinfoService {

    private final AgricultureBaseinfoMapper baseMapper;

    /**
     * 查询基地信息
     *
     * @param baseId 主键
     * @return 基地信息
     */
    @Override
    public AgricultureBaseinfoVo queryById(Long baseId){
        return baseMapper.selectVoById(baseId);
    }

    /**
     * 分页查询基地信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 基地信息分页列表
     */
    @Override
    public TableDataInfo<AgricultureBaseinfoVo> queryPageList(AgricultureBaseinfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AgricultureBaseinfo> lqw = buildQueryWrapper(bo);
        Page<AgricultureBaseinfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的基地信息列表
     *
     * @param bo 查询条件
     * @return 基地信息列表
     */
    @Override
    public List<AgricultureBaseinfoVo> queryList(AgricultureBaseinfoBo bo) {
        LambdaQueryWrapper<AgricultureBaseinfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AgricultureBaseinfo> buildQueryWrapper(AgricultureBaseinfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AgricultureBaseinfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getBaseShortName()), AgricultureBaseinfo::getBaseShortName, bo.getBaseShortName());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), AgricultureBaseinfo::getBaseCode, bo.getBaseCode());
        lqw.like(StringUtils.isNotBlank(bo.getBaseName()), AgricultureBaseinfo::getBaseName, bo.getBaseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseLeader()), AgricultureBaseinfo::getBaseLeader, bo.getBaseLeader());
        lqw.eq(StringUtils.isNotBlank(bo.getLeaderTel()), AgricultureBaseinfo::getLeaderTel, bo.getLeaderTel());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseAddress()), AgricultureBaseinfo::getBaseAddress, bo.getBaseAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseArea()), AgricultureBaseinfo::getBaseArea, bo.getBaseArea());
        lqw.eq(bo.getBaseAltitude() != null, AgricultureBaseinfo::getBaseAltitude, bo.getBaseAltitude());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseImg()), AgricultureBaseinfo::getBaseImg, bo.getBaseImg());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseDes()), AgricultureBaseinfo::getBaseDes, bo.getBaseDes());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AgricultureBaseinfo::getStatus, bo.getStatus());
        lqw.eq(bo.getOrderNum() != null, AgricultureBaseinfo::getOrderNum, bo.getOrderNum());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCoordinate()), AgricultureBaseinfo::getBaseCoordinate, bo.getBaseCoordinate());
        return lqw;
    }

    /**
     * 新增基地信息
     *
     * @param bo 基地信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AgricultureBaseinfoBo bo) {
        AgricultureBaseinfo add = MapstructUtils.convert(bo, AgricultureBaseinfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBaseId(add.getBaseId());
        }
        return flag;
    }

    /**
     * 修改基地信息
     *
     * @param bo 基地信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AgricultureBaseinfoBo bo) {
        AgricultureBaseinfo update = MapstructUtils.convert(bo, AgricultureBaseinfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AgricultureBaseinfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除基地信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
